﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Drawing.Text;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Web.Hosting;
using System.Web.Http;
using ezCloud.Data;
using Newtonsoft.Json.Linq;
using ezCloud.Api.Common;
using System.Threading.Tasks;

namespace ezCloud.Api.Controllers
{
    [RoutePrefix("api/Language")]
    public class LanguageController : ezController
    {
        string CodeToString(string code)
        {
            return char.ToUpper(code[0]) + code.Substring(1).ToLower().Replace('_', ' ');
        }
        [Route("Notify")]
        public async Task<IHttpActionResult> NewLanguageCodeDetected(string code)
        {
            
            var languageItem = context.LanguageItems.Where(item => item.LanguageCode.ToUpper() == code.ToUpper()).FirstOrDefault();
            if (languageItem == null)
            {
                //
                LanguageItems li = new LanguageItems();
                li.LanguageCode = code;
                li.CreatedDate = DateTime.UtcNow;
                context.LanguageItems.InsertOnSubmit(li);
                context.SubmitChanges();

                var langs = context.Languages.ToList();
                foreach(var lang in langs) {                   
                    LanguageItemValues liv = new LanguageItemValues();
                    liv.LanguageId = lang.LanguageId;
                    liv.LanguageItemId = li.LanguageItemId;
                    liv.LanguageItemValue = CodeToString(code);
                    context.LanguageItemValues.InsertOnSubmit(liv);
                    context.SubmitChanges();
                }
            }
            else
            {
                var langs = context.Languages.ToList();
                foreach (var lang in langs)
                {
                    //var lang = context.Languages.Where(l => l.LanguageCode == "en").FirstOrDefault();
                    var olditem = context.LanguageItemValues.Where(livi => livi.LanguageId == lang.LanguageId && livi.LanguageItemId == languageItem.LanguageItemId).FirstOrDefault();
                    if (olditem == null)
                    {
                        LanguageItemValues liv = new LanguageItemValues();
                        liv.LanguageId = lang.LanguageId;
                        liv.LanguageItemId = languageItem.LanguageItemId;
                        liv.LanguageItemValue = CodeToString(code);
                        context.LanguageItemValues.InsertOnSubmit(liv);
                        context.SubmitChanges();
                    }
                    
                }
            }
            return Ok();
        }
        [Route("Get")]
        public async Task<IHttpActionResult> GetLanguage(string lang)
        {
            
            var langObj = context.Languages.Where(item => item.LanguageCode == lang).FirstOrDefault();
            if (langObj != null)
            {
                var items = context.LanguageItemValues.Where(v => v.LanguageId == langObj.LanguageId).Select(v => new { Key = v.LanguageItems.LanguageCode, Value = v.LanguageItemValue }).ToList();
                var resourceObj = new JObject();
                foreach (var i in items)
                {
                    resourceObj.Add(i.Key, i.Value);
                }

                return Ok(resourceObj);
            }
            return BadRequest("No Language Found");
        }
        [Route("BatchCreate")]
        public async Task<IHttpActionResult> BatchCreate(string languageCode, List<LanguageItem> items)
        {
            
            var lang = context.Languages.Where(l => l.LanguageCode == languageCode).FirstOrDefault();
            if (lang == null) return BadRequest("No Language");
            foreach (var item in items) {
                var litem = context.LanguageItems.Where(li => li.LanguageCode == item.Key).FirstOrDefault();
                if (litem == null)
                {
                    LanguageItems newItem = new LanguageItems();
                    newItem.LanguageCode = item.Key;
                    context.LanguageItems.InsertOnSubmit(newItem);
                    context.SubmitChanges();
                    LanguageItemValues newItemValues = new LanguageItemValues();
                    newItemValues.LanguageId = lang.LanguageId;
                    newItemValues.LanguageItemId = newItem.LanguageItemId;
                    newItemValues.LanguageItemValue = item.Value;
                    context.LanguageItemValues.InsertOnSubmit(newItemValues);
                    context.SubmitChanges();
                }
                else
                {
                    var olv = context.LanguageItemValues.Where(liv => liv.LanguageId == lang.LanguageId && liv.LanguageItemId == litem.LanguageItemId).FirstOrDefault();
                    if (olv == null)
                    {
                        //Insert new
                        LanguageItemValues newItemValues = new LanguageItemValues();
                        newItemValues.LanguageId = lang.LanguageId;
                        newItemValues.LanguageItemId = litem.LanguageItemId;
                        newItemValues.LanguageItemValue = item.Value;
                        context.LanguageItemValues.InsertOnSubmit(newItemValues);
                        context.SubmitChanges();
                    }
                }
            }
            return Ok(items);
        }
        
        public class LanguageItem
        {
            public string Key { get; set; }
            public string Value { get; set; }
        }
    }
}
